package kr.hkit.powerjava.chapter26.Young;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class DepartmentService extends AbsSerivce<Department> {

	public DepartmentService(Scanner sc, Connection connection) {
		super(sc, connection);
		dao = new DepartmentDAO(connection);
	}

	@Override
	public Department inputItemInfo(boolean gubun) {
		System.out.println("부서번호 부서명 ");

		Department dept = new Department(sc.nextInt(), sc.next().trim());

		if (gubun) {
			System.out.println("부서위치");
			dept.setFloor(sc.nextInt()); 
		}

		return dept;
	}

	@Override
	public void prnSubMenu() {

		ArrayList<Department> list = null;

		System.out.println("1.부서명순 정렬 2.위치순 정렬 3.부서번호순 정렬 ");
		try {
			list = dao.listItems();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		int res = sc.nextInt();
		System.out.println("=========================="+"");
		System.out.printf("%3s\t%5s\t%2s\n","부서번호", "부서명", "부서위치");
		switch (res) {
			case 1:
				prnItemList(new Comparator<Department>() {

					@Override
					public int compare(Department o1, Department o2) {
						return o1.getDeptName().hashCode()
								- o2.getDeptName().hashCode();
					}
				}, list);
				break;

			case 2:
				prnItemList(new Comparator<Department>() {

					@Override
					public int compare(Department o1, Department o2) {
						return o1.getFloor() - o2.getFloor();
					}
				}, list);
				break;
			default:
				prnItemList(null, list);
				break;
		}
	}
}
